package com.woniu.p2p.utils;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * 发送邮件
 *
 *
 */
public class EmailTool {

    public static int sendMail(String email) {
        int mobileCode = (int)((Math.random()*9+1)*100000);
        try {
            final Properties props = new Properties();
            props.put("mail.user", "1160610144@qq.com");
            props.put("mail.password", "nhfvnxnxeznahajg"); // 授权码
            props.put("mail.smtp.auth", "true");
            props.put("mail.transport.protocol", "smtp");
            props.put("mail.smtp.host", "smtp.qq.com");
            props.put("mail.smtp.port", "25");

            Session mailSession = Session.getDefaultInstance(props);

            Message msg = new MimeMessage(mailSession);
            msg.setFrom(new InternetAddress("1160610144@qq.com")); // 发件人
            msg.addRecipients(Message.RecipientType.TO, InternetAddress.parse(email)); // 收件人
            msg.setSubject("用户邮件认证-中兴财富"); // 主题
            msg.setContent(
                    "<h1>此邮件为中兴财富电商平台激活邮件！</h1><br />"
                            + "<h4>验证码是"+mobileCode+"</h4>",
                    "text/html;charset=UTF-8");
            msg.saveChanges();

            Transport transport = mailSession.getTransport("smtp");
            transport.connect(props.getProperty("mail.smtp.host"), props.getProperty("mail.user"),
                    props.getProperty("mail.password"));
            transport.sendMessage(msg, msg.getAllRecipients());
            transport.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return mobileCode;
    }

    public static void main(String[] args) {
        int i = sendMail("1160610144@qq.com");
        System.out.println(i);
    }
}
